import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './App.vue';
import routes from './router';

Vue.config.productionTip = false;

// 声明变量管理vue及路由实例
let router = null;
let instance = null;
const __qiankun__ = window.__POWERED_BY_QIANKUN__;

if (window.__POWERED_BY_QIANKUN__) {
  // eslint-disable-next-line
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}

// 导出子应用生命周期 挂载前
export async function bootstrap(props) {
  console.log(props)
}

// 导出子应用生命周期 挂载后
export async function mount(props) {
  console.log('子应用mount', props)
  // 设置通讯
  Vue.prototype.$onGlobalStateChange = props.onGlobalStateChange
  Vue.prototype.$setGlobalState = props.setGlobalState

  // 实例化router
  router = new VueRouter({
    base:  '/', // 也可写子应用路径
    // mode: 'history',
    routes // 通过主应用传递的props来获取，可实现动态路由
  });
  // 实例化子应用
  instance = new Vue({
    router,
    render: h => h(App)
  }).$mount('#app');
}

// 导出子应用生命周期 卸载后
export async function unmount() {
  instance.$destroy();
  instance = null;
  router = null;
}

// 单独开发环境
__qiankun__ || mount();